package day17.method;

public class ThreadMethod02 {
    public static void main(String[] args) throws InterruptedException {
        T2 t2 = new T2();
        t2.start();

        for (int i = 1; i < 20; i++) {
            Thread.sleep(1000);
            System.out.println("主线程（小弟）吃了"+i+"包子");
            if(i == 5){
                System.out.println("主线程(小弟) 让 子线程(老大) 先吃");
                t2.join();//线程插队，这里相当于让t2 线程先执行完毕
                //Thread.yield();礼让，不一定成功
                System.out.println("老大吃完了，小弟接着吃");
            }
        }
    }
}

class T2 extends Thread {
    @Override
    public void run() {
        for (int i = 1; i <= 20; i++) {
            try {
                Thread.sleep(1000);//休眠1秒
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("子线程(老大) 吃了 " + i + " 包子");
        }
    }
}
